package com.ss.android.newmedia;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;


import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.ss.android.account.Spipe;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.app.AppHooks;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.d.SSDBHelper;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.model.ItemActionV3;
import com.ss.android.model.ItemAction;
import com.ss.android.model.f;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BatchActionService extends Service {
    final class a extends AbsApiThread {
        private final BatchActionService b;
        private Context a;

        public a(BatchActionService arg2, Context arg3) {
            super("BatchSyncThread");
            this.b = arg2;
            this.a = arg3.getApplicationContext();
        }

        private long a(long arg12) {
            boolean v0_4;
            int v0_3;
            f v4;
            f v0 = null;
            if (this.b.a != null) {
                SSDBHelper v1 = this.b.a.getDBHelper();
                v4 = v1 != null ? v1.b(arg12) : v0;
            } else {
                v4 = v0;
            }

            if (v4 != null) {
                long v2 = v4.mTime;
                if (v4 != null && !android.support.design.a.isTextEmpty(v4.mUrl)) {
                    Logger.i("BatchActionService", "开始发送请求:" + v4.toString());
                    if (v4.mRequestMethod == 1) {
                        ArrayList v5 = new ArrayList();
                        if (v4.mEentityJson != null) {
                            try {
                                JSONObject v6 = new JSONObject(v4.mEentityJson);
                                Iterator v7 = v6.keys();
                                while (true) {
                                    if (v7 == null) {
                                        break;
                                    } else if (v7.hasNext()) {
                                        Object v0_2 = v7.next();
                                        ((List) v5).add(new NameValueImpl(((String) v0_2), v6.optString(((String) v0_2))));
                                        continue;
                                    } else {
                                        break;
                                    }
                                }
                            } catch (Exception v0_1) {
                                Logger.throwExceptionInDebug(v0_1);
                            }
                        }
                        v0_3 = 1048576;
                        try {
                            v0_4 = isApiSuccess(NetworkUtils.executePost(v0_3, v4.mUrl, ((List) v5)));
                        } catch (Exception v0_1) {
                            v0_1.printStackTrace();
                            v0_4 = false;
                        }
                    } else {
                        v0_3 = 1048576;
                        try {
                            v0_4 = isApiSuccess(NetworkUtils.doGet(v0_3, v4.mUrl));
                        } catch (Exception v0_1) {
                            v0_1.printStackTrace();
                            v0_4 = false;
                        }
                    }

                    label_61:
                    if (v0_4) {
                        this.b.a.a(v4);
                        Logger.i("BatchActionService", "发送成功; " + v4.mUrl);
                    } else {
                        Logger.i("BatchActionService", "发送失败; " + v4.mUrl + ";已重试的次数:" + v4.retry_count);
                        ++v4.retry_count;
                        if (v4.retry_count > 5) {
                            this.b.a.a(v4);
                        } else {
                            this.b.a.b(v4);
                        }
                    }

                    if (arg12 >= v2) {
                        Logger.throwException("sendNetRequest : next_min_time >= max_time: " + arg12 + " " + v2);
                        return v2;
                    }

                    return v2;

                }

                this.b.a.a(v4);
                arg12 = v2;
                return arg12;
            } else {
                return arg12;
            }
        }

        private void a() {
            long v0;
            long v2 = 0L;
            for (v0 = 0; !this.isCanceled(); v0 = v2) {
                if (this.a == null) {
                    return;
                }

                if (!NetworkUtils.isNetworkAvailable(this.a)) {
                    return;
                }

                v2 = this.a(v0);
                if (v2 == v0) {
                    return;
                }
            }
        }

        private boolean a(String arg7) {
            boolean v0 = false;
            if (this.a != null && ((NetworkUtils.isNetworkAvailable(this.a)) && !android.support.design.a.isTextEmpty(arg7))) {
                ArrayList v4 = new ArrayList();
                ((List) v4).add(new NameValueImpl("action_unit", arg7));
                ((List) v4).add(new NameValueImpl("item_version", "2"));
                int v3 = 0;
                while (v3 < 2) {
                    int v2 = 8192;
                    try {
                        String v2_2 = NetworkUtils.executePost(v2, Spipe.dataBatchItemActionUrl, ((List) v4));
                        if (v2_2 == null) {
                            return v0;
                        }

                        if (v2_2.length() == 0) {
                            return v0;
                        }

                        return isApiSuccess(new JSONObject(v2_2));
                    } catch (Throwable v2_1) {
                        if ((v2_1 instanceof HttpResponseException)) {
                            return true;
                        }

                        ++v3;
                        continue;
                    }
                }

                v0 = true;
            }

            return v0;
        }

        private boolean a(JSONArray arg7) {
            boolean v0 = false;
            if (this.a != null && ((NetworkUtils.isNetworkAvailable(this.a)) && arg7.length() > 0)) {
                JSONObject v1 = new JSONObject();
                try {
                    v1.put("actions", arg7);
                    JSONObject v2 = AppLog.f(this.a).timeSync;
                    if (v2 != null) {
                        v1.put("time_sync", v2);
                    }

                    String v1_2 = NetworkUtils.executePost(-1, Spipe.userDataBatchActionUrl, v1.toString().getBytes("UTF-8"), NetworkUtils.CompressType.GZIP, "application/json; charset=utf-8");
                    if (v1_2 == null) {
                        return v0;
                    }

                    if (v1_2.length() == 0) {
                        return v0;
                    }

                    v0 = isApiSuccess(new JSONObject(v1_2));
                } catch (Throwable v1_1) {
                    Logger.e("BatchActionService", "throwable in doSendActionsV3 : " + v1_1.toString());
                }
            }

            return v0;
        }

        private long b(long arg14) {
            List v4;
            List v0 = null;
            if (this.b.a != null) {
                SSDBHelper v1 = this.b.a.getDBHelper();
                v4 = v1 != null ? v1.getPendingActionsV3(arg14) : v0;
            } else {
                v4 = v0;
            }

            if (v4 != null && !v4.isEmpty()) {
                JSONArray v5 = new JSONArray();
                Iterator v6 = v4.iterator();
                long v2 = 0;
                while (v6.hasNext()) {
                    Object v0_1 = v6.next();
                    if (!((ItemActionV3) v0_1).a()) {
                        continue;
                    }

                    if (v2 < ((ItemActionV3) v0_1).timestamp) {
                        v2 = ((ItemActionV3) v0_1).timestamp;
                    }

                    long v8 = ((ItemActionV3) v0_1).timestamp / 1000;
                    JSONObject v1_1 = new JSONObject();
                    try {
                        v1_1.put("action", ((ItemActionV3) v0_1).action);
                        v1_1.put("type", ((ItemActionV3) v0_1).targetType);
                        if (((ItemActionV3) v0_1).d != null) {
                            v1_1.put("id", ((ItemActionV3) v0_1).d.groupId);
                            v1_1.put("item_id", ((ItemActionV3) v0_1).d.itemId);
                            v1_1.put("aggr_type", ((ItemActionV3) v0_1).d.aggrType);
                        }

                        v1_1.put("timestamp", v8);
                        if (((ItemActionV3) v0_1).b()) {
                            v1_1.put("filter_words", ((ItemActionV3) v0_1).filterWords);
                        }

                        if (((ItemActionV3) v0_1).c()) {
                            v1_1.put("extra", ((ItemActionV3) v0_1).extra);
                        }

                        v5.put(v1_1);
                    } catch (JSONException v0_2) {
                        Logger.e("BatchActionService", "exception in sendActionV3 : " + v0_2.toString());
                    }
                }

                if (arg14 >= v2) {
                    Logger.w("BatchActionService", "sendActionV3 : next_min_time >= max_time: " + arg14 + " " + v2);
                    return arg14;
                }

                if ((this.a(v5)) && this.b.a != null) {
                    SSDBHelper v0_3 = this.b.a.getDBHelper();
                    if (v0_3 != null) {
                        v0_3.deleteItemActionV3List(v4);
                    }
                }

                arg14 = v2;
            }

            return arg14;
        }

        private void b() {
            long v0;
            long v2 = 0L;
            for (v0 = 0; !this.isCanceled(); v0 = v2) {
                v2 = this.b(v0);
                if (v2 == v0) {
                    return;
                }
            }
        }

        public final void run() {
            long v2_2;
            List v8;
            SSDBHelper v2;
            Logger.i("BatchActionService", "start batch_item_action");
            long v4;
            for (v4 = 0; !this.isCanceled(); v4 = v2_2) {
                if (this.b.a != null) {
                    v2 = this.b.a.getDBHelper();
                    v8 = v2 != null ? v2.queryItemActionList(v4) : null;
                } else {
                    v8 = null;
                }

                if (v8 == null || (v8.isEmpty())) {
                    v2_2 = v4;
                } else {
                    StringBuilder v9 = new StringBuilder();
                    Iterator v10 = v8.iterator();
                    int v3 = 1;
                    long v6 = 0;
                    while (v10.hasNext()) {
                        Object v2_1 = v10.next();
                        if (((ItemAction) v2_1).a == null) {
                            continue;
                        }

                        if (((ItemAction) v2_1).a.groupId <= 0) {
                            continue;
                        }

                        if (((ItemAction) v2_1).action <= 0) {
                            continue;
                        }

                        if (((ItemAction) v2_1).timestamp <= 0) {
                            continue;
                        }

                        if (v6 < ((ItemAction) v2_1).timestamp) {
                            v6 = ((ItemAction) v2_1).timestamp;
                        }

                        String v11 = Spipe.a(((ItemAction) v2_1).action);
                        if (v11 == null) {
                            continue;
                        }

                        long v12 = ((ItemAction) v2_1).timestamp / 1000;
                        if (v3 != 0) {
                            v3 = 0;
                        } else {
                            v9.append("|");
                        }

                        v9.append(v11);
                        v9.append("-").append(((ItemAction) v2_1).a.groupId).append("-").append(((ItemAction) v2_1).a.itemId).append("-").append(((ItemAction) v2_1).a.aggrType).append("-").append(v12);
                    }

                    if (v4 >= v6) {
                        Logger.w("BatchActionService", "sendActionV2 : next_min_time >= max_time: " + v4 + " " + v6);
                        v2_2 = v4;
//                        goto label_22;
                    }else {
                        if ((this.a(v9.toString())) && this.b.a != null) {
                            v2 = this.b.a.getDBHelper();
                            if (v2 != null) {
                                v2.deleteItemAction(v8);
                            }
                        }

                        v2_2 = v6;
                    }
                }

                label_22:
                if (v2_2 == v4) {
                    break;
                }
            }

            this.b();
            this.a();
            if (this.b.b == this) {
                this.b.b = null;
            }

            Logger.i("BatchActionService", "stop batch_item_action");
            if (this.b.b == null && !this.b.c) {
                this.b.stopSelf();
            }
        }
    }

    BaseAppData a;
    a b;
    boolean c;

    public BatchActionService() {
        this.c = false;
    }

    private void a() {
        Logger.i("BatchActionService", "onService");
        if (this.b == null) {
            this.b = new a(this, ((Context) this));
            this.b.start();
        }
    }

    public IBinder onBind(Intent arg2) {
        throw new UnsupportedOperationException();
    }

    public void onCreate() {
        new WeakReference(this);
        this.a = BaseAppData.inst();
        AppHooks.InitHook v0 = AppHooks.getInitHook();
        if (v0 != null) {
            v0.tryInit(((Context) this));
        }
    }

    public void onDestroy() {
        if (this.b != null) {
            this.b.cancel();
            this.b = null;
        }

        this.c = true;
    }

    public void onStart(Intent arg1, int arg2) {
        this.a();
    }

    public int onStartCommand(Intent arg2, int arg3, int arg4) {
        this.a();
        return 2;
    }
}

